package pers.amos.eshop.advance.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.amos.eshop.advance.product.common.DataTypeConstants;
import pers.amos.eshop.advance.product.common.EventTypeConstants;
import pers.amos.eshop.advance.product.mapper.ProductPropertyMapper;
import pers.amos.eshop.advance.product.model.ProductProperty;
import pers.amos.eshop.advance.product.mq.RabbitMQSender;
import pers.amos.eshop.advance.product.service.ProductPropertyService;


@Service
public class ProductPropertyServiceImpl implements ProductPropertyService {

    @Autowired
    private ProductPropertyMapper productPropertyMapper;

    @Autowired
    private RabbitMQSender rabbitMQSender;

    @Override
    public void add(ProductProperty productProperty, String operationType) {
        productPropertyMapper.add(productProperty);

        String sendMessage = RabbitMQSender.buildProductDimData(EventTypeConstants.ADD_EVENT_TYPE,
                DataTypeConstants.PRODUCT_PROPERTY_DATA_TYPE,
                productProperty.getId(), productProperty.getProductId());

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    @Override
    public void update(ProductProperty productProperty, String operationType) {
        productPropertyMapper.update(productProperty);

        String sendMessage = RabbitMQSender.buildProductDimData(EventTypeConstants.UPDATE_EVENT_TYPE,
                DataTypeConstants.PRODUCT_PROPERTY_DATA_TYPE,
                productProperty.getId(), productProperty.getProductId());

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    @Override
    public void delete(Long id, String operationType) {
        productPropertyMapper.delete(id);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.DELETE_EVENT_TYPE,
                DataTypeConstants.PRODUCT_PROPERTY_DATA_TYPE, id);

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    @Override
    public ProductProperty findById(Long id) {
        return productPropertyMapper.findById(id);
    }

    @Override
    public ProductProperty findByProductId(Long productId) {
        return productPropertyMapper.findByProductId(productId);
    }


}
